package com.linkgie.galaxyframework.httpproxy.converters;

import com.linkgie.galaxyframework.httpproxy.ResponseHandler;
import com.linkgie.galaxyframework.httpproxy.ServiceInvocationContext;
import com.linkgie.galaxyframework.httpproxy.ServiceRequest;
import com.linkgie.galaxyframework.httpproxy.ServiceResponse;
import com.linkgie.galaxyframework.utils.json.JSONUtils;

public class JsonResponseHandler implements ResponseHandler {

	private Class<?> clazz;

	public JsonResponseHandler(Class<?> clazz) {
		this.clazz = clazz;
	}

	@Override
	public Object handleResponse(ServiceRequest request, ServiceResponse response,
			ServiceInvocationContext serviceContext) throws Exception {
		String jsonResponse = (String) StringResponseHandler.INSTANCE.handleResponse(request, response, serviceContext);
		if (jsonResponse == null) {
			return null;
		}
		jsonResponse = jsonResponse.trim();
		
		// TODO: 未指定“日期时间”格式的策略；
		// return JSONSerializeUtils.deserializeAs(jsonResponse, clazz);
		return JSONUtils.toValue(jsonResponse, clazz);
	}

}
